package com.lumm.services.impl;

import com.lumm.dao.StaffMapper;
import com.lumm.model.ResponseObject;
import com.lumm.model.Staff;
import com.lumm.model.StaffExample;
import com.lumm.model.User;
import com.lumm.services.AdminService;
import com.lumm.util.Constants;
import com.lumm.util.EmailUtil;
import com.lumm.util.FileUtil;
import com.lumm.util.PasswordUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.lumm.util.FileUtil.getCellFormatValue;

@Service("AdminServiceImp")
public class AdminServiceImpl implements AdminService {
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public Staff login(String id, String password) {
        StaffExample staffExample = new StaffExample();
        if (EmailUtil.checkEmailFormat(id)) {
            staffExample.or().andEmailEqualTo(id);
        } else {
            try {
                staffExample.or().andStaffIdEqualTo(Integer.parseInt(id));
            }
            catch (NumberFormatException e){
                e.printStackTrace();
                return null;
            }
        }
        List<Staff> staffs = staffMapper.selectByExample(staffExample);

        if (staffs.size() < 1)
            return null;

        Staff staff = staffs.get(0);

        if (PasswordUtil.encryptPassword(password).equals(staff.getPassword()))
            return staffs.get(0);
        else
            return null;
    }

    public Staff getStaffByStaffId(int staffId) {
        return staffMapper.selectByPrimaryKey(staffId);
    }

    public int insert(Staff user) {
        return staffMapper.insert(user);
    }

    @Override
    public Staff createNewStaffInfo(int staffId, String name, String password, String email, String gender, short type) {

        Staff newStaff = new Staff();
        newStaff.setStaffId(staffId);
        newStaff.setName(name);
        newStaff.setUserName(name);
        newStaff.setPassword(PasswordUtil.encryptPassword(password));
        newStaff.setEmail(email);
        newStaff.setGender(gender);
        newStaff.setType(type);

        staffMapper.insert(newStaff);

        StaffExample staffExample=new StaffExample();
        staffExample.or().andEmailEqualTo(email);

        List<Staff> staffs=staffMapper.selectByExample(staffExample);

        newStaff=staffs.size()>0?staffs.get(0):null;

        return newStaff;
    }

    @Override
    public int updateStaffInfo(int staffId, String name, String password, String gender, short type) {


        Staff staff=staffMapper.selectByPrimaryKey(staffId);

        if (staff==null)
            return 0;
        staff.setUserName(name);
        staff.setName(name);
        staff.setStaffId(staffId);
        staff.setType(type);
        staff.setGender(gender);
        staff.setPassword(PasswordUtil.encryptPassword(password));

        return staffMapper.updateByPrimaryKey(staff);
    }

    @Override
    public List<Staff> getStaffList(int offset, int count) {
        StaffExample staffExample = new StaffExample();
        staffExample.setOrderByClause("staff_id desc limit "+offset+","+count);
        return staffMapper.selectByExample(staffExample);
    }

    @Override
    public int deleteStaffInfo(int staffId) {
        return staffMapper.deleteByPrimaryKey(staffId);
    }

    @Override
    public int getStaffCount() {
        StaffExample staffExample = new StaffExample();
        staffExample.setOrderByClause("staff_id");
        return staffMapper.countByExample(staffExample);
    }


}